Now that you have a better understanding of how the Page type allows you to interact with the incoming HTTP request, the next step is to see how to interact with the outgoing HTTP response. In ASP.NET, the Response property of the Page class provides access to an instance of the HttpResponse type. This type defines a number of properties that allow you to format the HTTP response sent back to the client browser. Table 32-6 lists some core properties.
Table 32-6. Properties of the HttpResponse Type
Property | Meaning in Life |
---|---|
Cache | Returns the caching semantics of the web page (see Chapter 34) |
ContentEncoding | Gets or sets the HTTP character set of the output stream |
ContentType | Gets or sets the HTTP MIME type of the output stream |
Cookies | Gets the HttpCookie collection that will be returned to the browser |
Output | Enables text output to the outgoing HTTP content body |
OutputStream | Enables binary output to the outgoing HTTP content body |
StatusCode | Gets or sets the HTTP status code of output returned to the client |
StatusDescription | Gets or sets the HTTP status string of output returned to the client |
SuppressContent | Gets or sets a value indicating that HTTP content will not be sent to the client |
Also, consider the partial list of methods supported by the HttpResponse type described in Table 32-7.
Table 32-7. Methods of the HttpResponse Type
Method | Meaning in Life |
---|---|
Clear() | Clears all headers and content output from the buffer stream |
End() | Sends all currently buffered output to the client and then closes the socket connection |
Flush() | Sends all currently buffered output to the client |
Redirect() | Redirects a client to a new URL |
Write() | Writes values to an HTTP output content stream |
WriteFile() | Writes a file directly to an HTTP content output stream |
Perhaps the most well-known aspect of the HttpResponse type is the ability to write content directly to the HTTP output stream. The HttpResponse.Write() method allows you to pass in any HTML tags and/or text literals. The HttpResponse.WriteFile() method takes this functionality one step further, in that you can specify the name of a physical file on the web server whose contents should be rendered to the output stream (this is quite helpful to quickly emit the contents of an existing *.htm file).
To illustrate, assume you have added another Button type to your current *.aspx file that implements the server-side Click event handler like so:
protected void btnHttpResponse_Click(object sender, EventArgs e) { Response.Write("<b>My name is:</b><br>"); Response.Write(this.ToString()); Response.Write("<br><br><b>Here was your last request:</b><br>"); Response.WriteFile("MyHTMLPage.htm"); }
The role of this helper function (which you can assume is called by some server-side event handler) is quite simple. The only point of interest is the fact that the HttpResponse.WriteFile() method is now emitting the contents of a server-side *.htm file within the root directory of the website.
Again, while you can always take this old-school approach and render HTML tags and content using the Write() method, this approach is far less common under ASP.NET than with classic ASP. The reason is (once again) due to the advent of server-side web controls. Thus, if you wish to render a block of textual data to the browser, your task is as simple as assigning a string to the Text property of a Label widget.
Another aspect of the HttpResponse type is the ability to redirect the user to a new URL:
protected void btnWasteTime_Click(object sender, EventArgs e) { Response.Redirect("http://www.facebook.com"); }
If this event handler is invoked via a client-side postback, the user will automatically be redirected to the specified URL.
Note The HttpResponse.Redirect() method will always entail a trip back to the client browser. If you simply wish to transfer control to an *.aspx file in the same virtual directory, the HttpServerUtility.Transfer() method, accessed via the inherited Server property, is more efficient.
So much for investigating the functionality of System.Web.UI.Page. I will examine the role of the System.Web.UI.Control base class in the next chapter. Next up, let’s examine the life and times of a Pagederived object.
Source Code The FunWithPageMembers website is included under the Chapter 32 subdirectory.